import ComponentApi from "@/api/components/component";
import * as Constants from "@/constants/constants";
import {utils} from "@/utils/utils";
import { Message } from "element-ui";
export default function (fileList,folder='common',size=5) {
    return new Promise(async (resolve, reject) => {
        const fileSizeOverflow = fileList.find(file => {
            if(!(file instanceof File)) file = file.raw
            if(file.size>1024*1024*size){
                Message.error(`文件大小不能超过${size}MB`)
                return false
            }
            return true
        })
        if (!fileSizeOverflow) {
            Message.error(`文件大小不能超过${size}MB`)
            reject()
        } else {
            const res = await ComponentApi.uploadOss()
            const ossClient = (await import('ali-oss')).default({
                region: Constants.OSS_REGION,
                accessKeyId: res.data.accessKeyId,
                accessKeySecret: res.data.accessKeySecret,
                stsToken: res.data.securityToken,
                bucket: Constants.OSS_BUCKET
            })
            const uploadList = []
            for (let file of fileList) {
                const initVal = file
                if(!(file instanceof File)) file = file.raw
                const fileName = file.name
                const fileType = fileName.split("\.")[1];
                const newName = utils.mathRand19() + "." + fileType;
                const storeAs = Constants.OSS_WEB_URL + folder + '/' + newName;
                ossClient.put(storeAs, file, {}).then(ret => {
                        if (ret.res.statusCode === 200) {
                            initVal.url = ret.url.replace(Constants.OSS_URL, Constants.OSS_REAL_URL);
                            uploadList.push(initVal)
                            if (uploadList.length == fileList.length) {
                                resolve(uploadList)
                                Message.success("上传成功")
                            }
                            return ret
                        } else {
                            Message.error("上传失败")
                            reject("上传失败")
                        }
                    }
                ).catch(err => {
                    Message.error(err)
                    reject(err)
                })

            }
        }
    })

}

/**
 * 上传微信小程序的图片
 */
export function uploadWeixinImg(fileList,folder='common',size=5) {
    return new Promise(async (resolve, reject) => {
        for(let file of fileList){
            if(!(file instanceof File)) file = file.raw
            if(file.size>1024*1024*size){
                Message.error(`文件大小不能超过${size}MB`)
                reject()
            }
        }
        const res = await ComponentApi.uploadOss()
        const ossClient = (await import('ali-oss')).default({
            region: Constants.OSS_REGION,
            accessKeyId: res.data.accessKeyId,
            accessKeySecret: res.data.accessKeySecret,
            stsToken: res.data.securityToken,
            bucket: Constants.OSS_BUCKET
        })
        const uploadList = []
        for (let file of fileList) {
            const initVal = file
            if(!(file instanceof File)) file = file.raw
            const fileName = file.name
            const fileType = fileName.split("\.")[1];
            const newName = utils.mathRand19() + "." + fileType;
            const storeAs = folder + '/' + fileName;
            ossClient.put(storeAs, file, {}).then(ret => {
                if (ret.res.statusCode === 200) {
                    initVal.url = ret.url.replace(Constants.OSS_URL, Constants.OSS_REAL_URL);
                    uploadList.push(initVal)
                    if (uploadList.length == fileList.length) {
                        resolve(uploadList)
                        Message.success("上传成功")
                    }
                    return ret
                } else {
                    Message.error("上传失败")
                    reject("上传失败")
                }
            }
            ).catch(err => {
                Message.error(err)
                reject(err)
            })

        }
    })

}
